home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 October / CHIP Turkiye Ekim 2000.iso / prog / naps / 04 / setup.exe / Gnucleus / Packet.cpp < prev    next >
C/C++ Source or Header  |  2000-06-30  |  9KB  |  375 lines

  1. /********************************************************************************
  2.  
  3.     Gnucleus - A node application for the Gnutella network
  4.     Copyright (C) 2000 John Marshall
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.     For support, questions, comments, etc...
  20.     E-Mail: 
  21.         swabby@c0re.net
  22.     
  23.     Address:
  24.         21 Cadogan Way
  25.         Nashua, NH, USA 03062
  26.  
  27. ********************************************************************************/
  28.  
  29. #include "stdafx.h"
  30.  
  31. // Get rid of ugly warnings
  32. #pragma warning (disable : 4786)
  33. #include <map>
  34.  
  35. // include version functions
  36. #pragma comment(lib, "Version.lib")
  37.  
  38. #include "Gnucleus.h"
  39. #include "Packet.h"
  40.  
  41.  
  42. #ifdef _DEBUG
  43. #undef THIS_FILE
  44. static char THIS_FILE[]=__FILE__;
  45. #define new DEBUG_NEW
  46. #endif
  47.  
  48. int QueryItem::SortBy = 0;
  49. int QueryItem::Reverse = 0;
  50.  
  51. XWORD flipX(XWORD in)
  52. {
  53.     XWORD out = {in.a, in.b, in.c ,in.d};
  54.  
  55.     return out;
  56. }
  57.  
  58. WORD flipW(WORD in)
  59. {
  60.     byte *out = (byte *) ∈
  61.  
  62.     byte temp = out[0];
  63.     out[0] = out[1];
  64.     out[1] = temp;
  65.  
  66.     return (WORD) *out;
  67. }
  68.  
  69. XWORD makeX(DWORD conv)
  70. {
  71.     XWORD *in = (XWORD *) &conv;
  72.     XWORD out = {in->a, in->b, in->c ,in->d};
  73.  
  74.     return out;
  75. }
  76.  
  77. DWORD makeD(XWORD conv)
  78. {
  79.     DWORD *in = (DWORD *) &conv;
  80.     DWORD out = *in;
  81.  
  82.     return out;
  83. }
  84.  
  85. CString IPtoStr(IP in)
  86. {
  87.     // AP: Profiling showed this taking alot of CPU time because it's called alot.  Much faster this way.
  88. //    char buffer[20];
  89. //    ::sprintf (buffer, "%d.%d.%d.%d", in.a, in.b, in.c, in.d);
  90.  
  91.     char * buffer;
  92.     buffer = inet_ntoa(*(in_addr *)&in);
  93.  
  94.     return buffer;
  95. }
  96.  
  97. IP StrtoIP(CString in)
  98. {
  99. /*
  100.     int a = 0, b = 0, c = 0, d = 0;
  101.     ::sscanf (in, "%d.%d.%d.%d", &a, &b, &c, &d);
  102. */
  103.  
  104.     IP out;
  105.     
  106. /*
  107.     out.a = a;
  108.     out.b = b;
  109.     out.c = c;
  110.     out.d = d;
  111. */
  112.  
  113.     out.S_addr = inet_addr(in);        // standard WinSock2 function
  114.  
  115.     return out;    
  116. }
  117.  
  118. CString WrdtoStr(WORD in)
  119. {
  120.     char buff[8];
  121.     
  122.     ::sprintf (buff, "%u", in);
  123.  
  124.     return buff;
  125. }
  126.  
  127. CString DWrdtoStr(DWORD in)
  128. {
  129.     char buff[16];
  130.  
  131.     ::sprintf (buff, "%u", in);
  132.  
  133.     return buff;
  134. }
  135.  
  136. CString CommaIze(CString in)
  137. {
  138.     if (in.GetLength() > 3)
  139.         return CommaIze(in.Left(in.GetLength() - 3)) + "," + in.Right(3);
  140.     else
  141.         return in;
  142. }
  143.  
  144. HICON GetIconFromName(CString File)
  145. {
  146.     DWORD dwBufLen = 255;
  147.  
  148.     HKEY hKey = NULL;
  149.     HKEY pKey = NULL;
  150.     HICON FileIcon = NULL;
  151.  
  152.     // Open icon key
  153.     if(RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT( File.Mid( File.ReverseFind('.'))), 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) 
  154.     {    
  155.         TCHAR szFileKey[255];
  156.         RegQueryValueEx(hKey, NULL, NULL, NULL, (LPBYTE)szFileKey, &dwBufLen);
  157.         
  158.         CString FileKey(szFileKey);
  159.  
  160.         FileKey += "\\DefaultIcon";
  161.         
  162.         // Use the default key to open the icon name dir in the registry
  163.         if(RegOpenKeyEx(HKEY_CLASSES_ROOT, FileKey, 0, KEY_QUERY_VALUE, &pKey) == ERROR_SUCCESS) 
  164.         {
  165.             TCHAR szPathKey[255];
  166.             dwBufLen = 255;
  167.             RegQueryValueEx(pKey, NULL, NULL, NULL, (LPBYTE)szPathKey, &dwBufLen);
  168.                 
  169.             CString PathKey(szPathKey);
  170.             int comma = PathKey.Find(',');
  171.                     
  172.             // Extract the icon
  173.             if(ExtractIconEx(PathKey.Mid(0, comma), atoi(PathKey.Mid(comma + 1)), NULL, &FileIcon, 1) == 0)
  174.                 FileIcon = NULL;
  175.             
  176.             RegCloseKey(pKey);
  177.         }
  178.         
  179.         RegCloseKey(hKey);
  180.     }
  181.  
  182.     // If we didn't find the icon display a default windows one
  183.     if(FileIcon == NULL)
  184.     {
  185.         TCHAR szSysDir[MAX_PATH];
  186.  
  187.         GetSystemDirectory(szSysDir, sizeof (szSysDir));
  188.         CString SysDir(szSysDir);
  189.  
  190.         // Check for .exe
  191.         CString testExe = File.Mid( File.ReverseFind('.'));
  192.         testExe.MakeLower();
  193.         
  194.         if(testExe == ".exe")
  195.             ExtractIconEx(SysDir + "\\shell32.dll", 2, NULL, &FileIcon, 1);
  196.         else
  197.             ExtractIconEx(SysDir + "\\shell32.dll", 0, NULL, &FileIcon, 1);
  198.     }
  199.  
  200.     return FileIcon;
  201. }
  202.  
  203. CString GetIconDesc(CString File)
  204. // Needs to be merged with the above function
  205. {
  206.     DWORD dwBufLen = 255;
  207.  
  208.     HKEY hKey = NULL;
  209.     HKEY pKey = NULL;
  210.         
  211.     CString IconDesc;
  212.  
  213.     // Open icon key
  214.     if(RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT( File.Mid( File.ReverseFind('.'))), 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) 
  215.     {    
  216.         TCHAR szFileKey[255];
  217.         RegQueryValueEx(hKey, NULL, NULL, NULL, (LPBYTE)szFileKey, &dwBufLen);
  218.         
  219.         CString FileKey(szFileKey);
  220.         
  221.         // Use the default key to open the icon name dir in the registry
  222.         if(RegOpenKeyEx(HKEY_CLASSES_ROOT, FileKey, 0, KEY_QUERY_VALUE, &pKey) == ERROR_SUCCESS) 
  223.         {
  224.             TCHAR szPathKey[255];
  225.             dwBufLen = 255;
  226.             RegQueryValueEx(pKey, NULL, NULL, NULL, (LPBYTE)szPathKey, &dwBufLen);
  227.                 
  228.             IconDesc = szPathKey;
  229.             
  230.             RegCloseKey(pKey);
  231.         }
  232.         
  233.         RegCloseKey(hKey);
  234.     }
  235.  
  236.     // If we didn't find the icon display a default windows one
  237.     if(IconDesc == "")
  238.     {
  239.         IconDesc = File.Mid( File.ReverseFind('.') + 1 );
  240.  
  241.         IconDesc.MakeUpper();
  242.  
  243.         IconDesc += " File";
  244.     }
  245.  
  246.     return IconDesc;
  247. }
  248.  
  249.  
  250. CImageList g_imageList;
  251.  
  252. std::map<CString,int> g_extensionIndexMap;
  253.  
  254. int GetIconIndexFromName (const CString& file)
  255. {
  256.     CString extension = file.Mid( file.ReverseFind('.'));
  257.  
  258.     extension.MakeLower ();
  259.  
  260.     std::map<CString,int>::iterator it;
  261.  
  262.     it = g_extensionIndexMap.find (extension);
  263.  
  264.     if (it != g_extensionIndexMap.end ())
  265.     {
  266.         return (*it).second;
  267.     }
  268.  
  269.     HICON icon = GetIconFromName (file);
  270.  
  271. //    ASSERT (icon);        // We'd better have something!
  272.  
  273.     int index = -1;
  274.     if (icon)
  275.     {
  276.         index = g_imageList.Add (icon);
  277.     }
  278.  
  279.     g_extensionIndexMap[extension] = index;
  280.  
  281.     return index;
  282. }
  283.  
  284. CImageList* GetSharedImageList ()
  285. {
  286.     static bool sInited = false;
  287.  
  288.     if (!sInited)
  289.     {
  290.         g_imageList.Create (16, 16, ILC_COLOR8 | TRUE, 0, 10);
  291.         sInited = true;
  292.     }
  293.  
  294.     return &g_imageList;
  295. }
  296.  
  297. bool SearchMatch(const char* _psz_search_for, const char* _psz_string_to_search)
  298. {
  299.     for (;;)
  300.     {                
  301.         switch (*_psz_search_for)
  302.         {                          
  303.         default:    
  304.             if (*_psz_search_for != *_psz_string_to_search)    // if different chars
  305.                 return false;
  306.             if (!*_psz_search_for)            // if equal and both null else fall through
  307.                 return true;
  308.         case '?':   
  309.             if (!*_psz_string_to_search)            // if at end
  310.                 return false;
  311.    
  312.             _psz_search_for++;
  313.             _psz_string_to_search++;
  314.  
  315.             break;                // ok, both advanced one
  316.         case '*':   
  317.             _psz_search_for++;
  318.    
  319.             for (;;)
  320.                 if ( SearchMatch(_psz_search_for, _psz_string_to_search) )    // if rest matches
  321.                     return true;
  322.                 else if (*_psz_search_for == '.' && *(_psz_search_for + 1) == '*')  // if ".*" or "*.*"
  323.                     return true;
  324.                 else if (!*_psz_string_to_search)        // if nothing left
  325.                     return false;   
  326.                 else
  327.                     ++_psz_string_to_search;            // try again one char further
  328.         }
  329.     }
  330. }
  331.  
  332. //////////////////////////////////////////////////////////////
  333. // Auther: Nathan Brown
  334. //
  335. // Returns the file version number for the passed file name
  336. DWORD64 util_GetVersionNumber(LPCTSTR lpstrFilename)
  337. {    
  338.     DWORD dwHandle, dwVersionLen;
  339.     char strSubBlock[] = "\\";        // the root block, VS_FIXEDFILEINFO
  340.     char strShortFilename[129];
  341.     WORD * lpData;
  342.     WORD * lplpBuffer;
  343.     UINT puLen;
  344.     DWORD64 dw64FileVersion;
  345.  
  346.     // shorten the path (short filenames)
  347.     if(strlen(lpstrFilename) > 127)
  348.     {
  349.         DWORD retStringLen = GetShortPathName(lpstrFilename, strShortFilename, 128);
  350.     }
  351.     else
  352.     {
  353.         strcpy(strShortFilename, lpstrFilename);
  354.     }
  355.  
  356.     dwVersionLen = GetFileVersionInfoSize(strShortFilename, &dwHandle);
  357.     lpData = new WORD[dwVersionLen];
  358.     if(!GetFileVersionInfo(strShortFilename, dwHandle, dwVersionLen, lpData))
  359.     {
  360.         return false;            // if not succede
  361.     }
  362.     if(!VerQueryValue(lpData, strSubBlock, (VOID**)&lplpBuffer, &puLen))
  363.     {
  364.         return false;
  365.     }
  366.     DWORD64 dwFileVersionLS = (((VS_FIXEDFILEINFO*)(lplpBuffer))->dwFileVersionLS);
  367.     DWORD64 dwFileVersionMS = (((VS_FIXEDFILEINFO*)(lplpBuffer))->dwFileVersionMS);
  368.     
  369.  
  370.     dw64FileVersion = (dwFileVersionLS) + (dwFileVersionMS << 32);
  371.  
  372.     delete [] lpData;
  373.  
  374.     return dw64FileVersion;
  375. }